package vsqlbuilder.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import vsqlbuilder.Utils;

public class SplashScreen extends JDialog {

	static final long serialVersionUID = 0L; // TODO [low]: temporary fix, must be
												// change to correct one

	private static final int SPLASH_WIDTH = 480;

	private static final int SPLASH_HEIGHT = 300;

	private static final int BORDER_THICKNESS = 2;

	private static final int TOP_MARGIN = 10;

	private static final int OFFSET_SHIFT = 83;

	private static final int LABEL_HEIGHT = 60;

	private static final Color BLACK_COLOR = new Color(60, 60, 60);

	private static final Font REGULAR_FONT = new Font("Dialog", Font.PLAIN, 12);

	private static final Font BOLD_FONT = new Font("Tahoma", Font.BOLD
			+ Font.ITALIC, 24);

	private static SplashScreen instance;

	private SplashScreen(Frame owner) {

		super(owner, true);

		setBackground(Color.WHITE);
		setUndecorated(true);

		setSize(SPLASH_WIDTH, SPLASH_HEIGHT);
		setLocationRelativeTo(null);

		MouseAdapter disposeOnClick = new MouseAdapter() {

			public void mouseClicked(MouseEvent event) {

				disposeSplash();
			}
		};
		addMouseListener(disposeOnClick);

		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		contentPane.add(getContent(), BorderLayout.CENTER);
	}

	@SuppressWarnings("serial")
	private Component getContent() {

		Icon mainLogo = Utils.getImageIcon("MainLogo.png");
		Icon applicationLogo = Utils.getImageIcon("Logo2.png");

		int margin = (SPLASH_WIDTH - mainLogo.getIconWidth()) / 2;
		int offsetX = margin + OFFSET_SHIFT;
		int offsetY = margin + mainLogo.getIconHeight() + TOP_MARGIN;

		JPanel panel = new JPanel() {

			protected void paintComponent(Graphics g) {

				Graphics2D g2 = (Graphics2D) g;
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
						RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

				super.paintComponent(g);

				g2.setColor(BLACK_COLOR);
				g2.fillRect(0, 0, SPLASH_WIDTH, BORDER_THICKNESS);
				g2.fillRect(0, 0, BORDER_THICKNESS, SPLASH_HEIGHT);
				g2.fillRect(0, SPLASH_HEIGHT - BORDER_THICKNESS, SPLASH_WIDTH,
						BORDER_THICKNESS);
				g2.fillRect(SPLASH_WIDTH - BORDER_THICKNESS, 0,
						BORDER_THICKNESS, SPLASH_WIDTH);
			}
		};
		panel.setLayout(null);
		panel.setBackground(Color.WHITE);

		JLabel mainLabel = new JLabel();
		mainLabel.setIcon(mainLogo);
		mainLabel.setBounds(margin, margin, mainLogo.getIconWidth(), mainLogo
				.getIconHeight());
		panel.add(mainLabel);

		JLabel titleLabel = new JLabel("Visual SQL Builder");
		titleLabel.setIconTextGap(6);
		titleLabel.setForeground(BLACK_COLOR);
		titleLabel.setIcon(applicationLogo);
		titleLabel.setFont(BOLD_FONT);
		titleLabel.setBounds(offsetX, offsetY, SPLASH_WIDTH - margin - offsetX,
				applicationLogo.getIconHeight());
		panel.add(titleLabel);

		JLabel descriptionLabel = new JLabel("<html>"
				+ "An application that visually build SQL Statments");

		descriptionLabel.setFont(REGULAR_FONT);
		descriptionLabel.setVerticalAlignment(SwingConstants.TOP);
		descriptionLabel.setBounds(offsetX, offsetY
				+ applicationLogo.getIconHeight() + 10, SPLASH_WIDTH - margin
				- offsetX, LABEL_HEIGHT);
		panel.add(descriptionLabel);

		JLabel copyrightLabel = new JLabel(
				"<html>"
						+ "License under LGPL terms or GPL terms.<br> <url><b>http://vsqlbuilder.sourceforge.net</b></url>"
						+ "<br>PRE-ALPHA Versión 0.3 <br>2008");

		copyrightLabel.setVerticalAlignment(SwingConstants.BOTTOM);
		copyrightLabel.setFont(REGULAR_FONT);
		copyrightLabel.setBounds(offsetX,
				SPLASH_HEIGHT - margin - LABEL_HEIGHT, SPLASH_WIDTH - margin
						- offsetX, LABEL_HEIGHT);
		panel.add(copyrightLabel);

		return panel;
	}

	public static void splash(Frame owner) {

		if (instance == null) {
			instance = new SplashScreen(owner);
			instance.setVisible(true);

		} else {
			instance.setVisible(true);
		}
	}

	public static void disposeSplash() {
		if (instance != null) {
			instance.setVisible(false);
		}
	}
}